<template>
  <div>
    <a-space :size="8">
      <a-button
        v-if="!record.contentUrl"
        v-permission="['pms:order:updateOrderBodyText']"
        type="text"
        @click="createContract"
        >生成正文</a-button
      >
      <a-button
        v-if="record.contentUrl"
        v-permission="['pms:order:updateOrderBodyText']"
        type="text"
        @click="editDocHandle"
        >编辑正文</a-button
      >
      <a-button
        v-if="record.contentUrl"
        v-permission="['pms:order:updateOrderBodyText']"
        type="text"
        @click="delDocHandle"
        >删除正文</a-button
      >
      <a-button v-if="record.contentUrl" type="text" @click="readDocHandle"
        >查看正文</a-button
      >
    </a-space>
  </div>
</template>

<script setup lang="ts">
  import { ref, defineProps, watch } from 'vue';
  import { Message } from '@arco-design/web-vue';
  import { info } from '@/common/utils/modal-promise';
  import { updateOrderBodyText } from '@/api/purchase-order/purchaseOrderPreparation';
  import emitter from '@/common/hooks/bus';
  import ntko from '@/common/service/ntko/index';

  const props = defineProps({
    // 渲染的index
    rowIndex: {
      type: Number,
      default: -1,
    },
    // 渲染的key
    renderKey: {
      type: String,
      default: '',
    },
    // 当前行数据
    record: {
      type: Object,
      default: () => ({}),
    },
  });
  const creatDocUrl = ref(props.record.mdsTempletUrl);
  const readDocUrl = ref(props.record.contentUrl);

  const docId = ref('');
  // 生成正文
  const createContract = () => {
    console.log('creatDocUrl.value', creatDocUrl.value);
    ntko.createContractDoc(creatDocUrl.value, {
      secretLevel: props.record.secretLevel,
      mId: props.record.id,
      bizType: '3',
      dataId: props.record.id,
    });
  };
  // 编辑正文
  const editDocHandle = async () => {
    docId.value = await ntko.editDoc(readDocUrl.value, {
      secretLevel: props.record.secretLevel,
      dataId: props.record.id, // 数据id，用于save之后，确认是哪一条数据
    });
  };
  const delDocHandle = async () => {
    await info({
      title: '您确定要删除正文？',
      content: '',
      hideCancel: false,
    });
    const params = {
      id: props.record.id,
      contentUrl: '',
    };
    const res1: any = await updateOrderBodyText(params);
    if (res1.success) {
      Message.success('删除成功！');
      emitter.emit('getTabData');
    }
  };
  // 查看正文
  const readDocHandle = async () => {
    docId.value = await ntko.readDoc(readDocUrl.value);
  };
  // 监听文档保存成功了
  ntko.on('save', async (res: any) => {
    const params = {
      id: res.dataId,
      contentUrl: res.data,
    };

    const res1: any = await updateOrderBodyText(params);
    if (res1.success) {
      emitter.emit('getTabData');
    }
  });

  // 监听文档保存成功了
  ntko.on('close', () => {
    console.log('1');
  });
  const nowVal = ref(props.rowIndex);
  watch(
    () => props.rowIndex,
    (newVal) => {
      nowVal.value = newVal;
    }
  );
</script>

<style scoped lang="less">
  :deep(.arco-btn-size-medium) {
    height: 16px !important;
    padding: 0 !important;
  }
</style>
